home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvidoc / dvidoc.c < prev    next >
C/C++ Source or Header  |  1994-04-24  |  42KB  |  1,828 lines

  1. /*
  2. ** The TeX text files device driver DVIDOC.WEB was originally written
  3. ** at OSU in April, 1983, by modifying the utility DVItype.
  4. ** Ken Yap (ken@cs.rochester.edu) modified it to work under 4.3 BSD.
  5. ** In January 1988 Neta Amit (amit@umn-cs.cs.umn.edu) tangle'd version-2
  6. ** (of July 1987). A little Icon program helped modify the resulting
  7. ** dvidoc.p into something acceptable to p2c, a Pascal-to-C transformer. 
  8. ** Dvidoc.c contained 3 errors that were fixed manually. 
  9. **
  10. ** Although the program was not tested extensively, theoretically it
  11. ** should behave just like the original dvidoc.
  12. */
  13. extern void    exit();
  14. /*
  15. **    Definitions for i/o
  16. */
  17. # include <stdio.h>
  18. typedef struct {
  19.     FILE    *fp;
  20.     unsigned short    eoln:1,
  21.             eof:1,
  22.             out:1,
  23.             init:1,
  24.             :12;
  25.     char    buf;
  26. }     text;
  27. text    input = { stdin, 0, 0 };
  28. text    output = { stdout, 0, 0 };
  29. # define Fread(x, f) fread((char *)&x, sizeof(x), 1, f)
  30. # define Get(f) Fread((f).buf, (f).fp)
  31. # define Getx(f) (f).init = 1, (f).eoln = (((f).buf = fgetc((f).fp)) == '\n') ? (((f).buf = ' '), 1) : 0
  32. # define Getchr(f) (f).buf, Getx(f)
  33. static FILE    *Tmpfil;
  34. static long    Tmplng;
  35. static double    Tmpdbl;
  36. # define Fscan(f) (f).init ? ungetc((f).buf, (f).fp) : 0, Tmpfil = (f).fp
  37. # define Scan(p, a) Scanck(fscanf(Tmpfil, p, a))
  38. void    Scanck();
  39. # define Fwrite(x, f) fwrite((char *)&x, sizeof(x), 1, f)
  40. # define Put(f) Fwrite((f).buf, (f).fp)
  41. # define Putx(f) (f).eoln = ((f).buf == '\n'), (void)fputc((f).buf, (f).fp)
  42. # define Putchr(c, f) (f).buf = (c), Putx(f)
  43. # define Putl(f, v) (f).eoln = v
  44. # define Finish(f) ((f).out && !(f).eoln) ? (Putchr('\n', f), 0) : 0, rewind((f).fp), 0
  45. # ifdef READONLY
  46. static char    Rmode[] = "r";
  47. # else
  48. static char    Rmode[] = "r+";
  49. # endif
  50. # define Reset(f, n) (f).init = (f).init ? (rewind((f).fp),0) : (((f).fp = Fopen(n, Rmode)), 1), (f).eof = (f).out = 0, Get(f)
  51. # define Resetx(f, n) (f).init = (f).init ? (Finish(f)) : (((f).fp = Fopen(n, Rmode)), 1), (f).eof = (f).out = 0, Getx(f)
  52. # ifdef WRITEONLY
  53. static char    Wmode[] = "w";
  54. # else
  55. static char    Wmode[] = "w+";
  56. # endif
  57. # define Rewrite(f, n) (f).init = (f).init ? rewind((f).fp) : (((f).fp = Fopen(n, Wmode)), 1), (f).out = (f).eof = 1
  58. # define Rewritex(f, n) (f).init = (f).init ? (Finish(f)) : (((f).fp = Fopen(n, Wmode)), 1), (f).out = (f).eof = (f).eoln = 1
  59. FILE    *Fopen();
  60. # define MAXFILENAME 256
  61. /*
  62. **    Definitions for case-statements
  63. **    and for non-local gotos
  64. */
  65. # define Line __LINE__
  66. void    Caseerror();
  67. # include <setjmp.h>
  68. static struct Jb { jmp_buf    jb; } J[1];
  69. /*
  70. **    Definitions for standard types
  71. */
  72. typedef char    boolean;
  73. # define false (boolean)0
  74. # define true (boolean)1
  75. extern char    *Bools[];
  76. typedef int    integer;
  77. typedef double    real;
  78. extern double fabs();
  79. extern char *strncpy();
  80. /*
  81. **    Definitions for argv-operations
  82. */
  83. int    argc;
  84. char    **argv;
  85.  void
  86. Argvgt(n, cp, l)
  87. int    n;
  88. register int    l;
  89. register char    *cp;
  90. {
  91.     register char    *sp;
  92.  
  93.     for (sp = argv[n]; l > 0 && *sp; l--)
  94.         *cp++ = *sp++;
  95.     while (l-- > 0)
  96.         *cp++ = ' ';
  97. }
  98. /*
  99. **    Start of program definitions
  100. */
  101. # define maxfonts 100
  102. # define maxwidths 10000
  103. # define terminallinelength 150
  104. # define stacksize 100
  105. # define namesize 1000
  106. # define namelength 100
  107. # define pagewidthmax 132
  108. # define pagelengthmax 88
  109. typedef unsigned char    asciicode;
  110. typedef struct {
  111.     FILE    *fp;
  112.     unsigned short    eoln:1,
  113.             eof:1,
  114.             out:1,
  115.             init:1,
  116.             :12;
  117.     char    buf;
  118. }     textfile;
  119. typedef integer    eightbits;
  120. typedef struct {
  121.     FILE    *fp;
  122.     unsigned short    eoln:1,
  123.             eof:1,
  124.             out:1,
  125.             init:1,
  126.             :12;
  127.     char    buf;
  128. }     bytefile;
  129. typedef struct { asciicode    A[128]; }    T59;
  130. typedef struct { char    A[255 + 1]; }    T60;
  131. typedef struct { char    A[namelength - 1 + 1]; }    T61;
  132. typedef struct { integer    A[maxfonts + 1]; }    T62;
  133. typedef struct { unsigned short    A[maxfonts + 1]; }    T63;
  134. typedef struct { asciicode    A[namesize + 1]; }    T64;
  135. typedef struct { integer    A[maxfonts + 1]; }    T65;
  136. typedef struct { integer    A[maxfonts + 1]; }    T66;
  137. typedef struct { integer    A[maxfonts + 1]; }    T67;
  138. typedef struct { integer    A[maxfonts + 1]; }    T68;
  139. typedef struct { integer    A[maxfonts + 1]; }    T69;
  140. typedef struct { integer    A[maxfonts + 1]; }    T70;
  141. typedef struct { integer    A[maxfonts + 1]; }    T71;
  142. typedef struct { integer    A[maxwidths + 1]; }    T72;
  143. typedef struct { integer    A[255 + 1]; }    T73;
  144. typedef struct { integer    A[maxwidths + 1]; }    T74;
  145. typedef struct { integer    A[9 + 1]; }    T75;
  146. typedef struct { boolean    A[9 + 1]; }    T76;
  147. typedef struct { integer    A[9 + 1]; }    T77;
  148. typedef struct { asciicode    A[terminallinelength + 1]; }    T78;
  149. typedef struct { char    A[9 - 1 + 1]; }    T79;
  150. typedef struct { struct { asciicode    A[pagelengthmax - 1 + 1]; }    A[pagewidthmax - 1 + 1]; }    T80;
  151. typedef struct { unsigned char    A[pagelengthmax - 1 + 1]; }    T81;
  152. typedef struct { integer    A[stacksize + 1]; }    T82;
  153. typedef struct { integer    A[stacksize + 1]; }    T83;
  154. T59    xord;
  155. T60    xchr;
  156. bytefile    dvifile;
  157. boolean    eofdvifile;
  158. bytefile    tfmfile;
  159. integer    curloc;
  160. T61    basename, curname, realnameoffile;
  161. eightbits    b0, b1, b2, b3;
  162. T62    fontnum;
  163. T63    fontname;
  164. T64    names;
  165. T65    fontchecksum;
  166. T66    fontscaledsize;
  167. T67    fontdesignsize;
  168. T68    fontspace;
  169. T69    fontbc;
  170. T70    fontec;
  171. T71    widthbase;
  172. T72    width;
  173. unsigned char    nf;
  174. unsigned short    widthptr;
  175. T73    inwidth;
  176. integer    tfmchecksum;
  177. T74    pixelwidth;
  178. real    horizconv;
  179. real    vertconv;
  180. real    truehorizconv;
  181. real    truevertconv;
  182. integer    numerator, denominator;
  183. integer    mag;
  184. integer    maxpages;
  185. real    horizresolution;
  186. real    vertresolution;
  187. integer    newmag;
  188. T75    startcount;
  189. T76    startthere;
  190. unsigned char    startvals;
  191. T77    count;
  192. T78    buffer;
  193. unsigned char    bufptr;
  194. boolean    inpostamble;
  195. T79    defaultdirectory;
  196. unsigned char    i;
  197. boolean    logerrors;
  198. textfile    errfile;
  199. T80    pagebuffer;
  200. T81    linehwm;
  201. unsigned char    pagehwm;
  202. integer    h, v, w, x, y, z,
  203.     hh, vv;
  204. T82    hstack, vstack, wstack, xstack, ystack, zstack;
  205. T83    hhstack, vvstack;
  206. integer    maxv;
  207. integer    maxh;
  208. integer    maxs;
  209. integer    maxvsofar, maxhsofar, maxssofar;
  210. integer    totalpages;
  211. integer    pagecount;
  212. integer    s;
  213. integer    ss;
  214. integer    curfont;
  215. integer    oldbackpointer;
  216. integer    newbackpointer;
  217. boolean    started;
  218. integer    postloc;
  219. integer    firstbackpointer;
  220. integer    startloc;
  221. integer    k, m, n, p, q;
  222.  
  223. extern void opendvifile();
  224.  
  225. extern integer getbyte();
  226.  
  227. extern integer signedbyte();
  228.  
  229. extern integer gettwobytes();
  230.  
  231. extern integer signedpair();
  232.  
  233. extern integer getthreebytes();
  234.  
  235. extern integer signedtrio();
  236.  
  237. extern integer signedquad();
  238.  
  239. extern integer curpos();
  240.  
  241. extern void setpos();
  242.  
  243. extern void setpaths();
  244.  
  245. extern boolean testaccess();
  246.  
  247. extern void exit();
  248.  
  249.  void
  250. initialize()
  251. {
  252.     register integer    i;
  253.  
  254.     setpaths();
  255.     {
  256.         integer    B1 = 0,
  257.             B2 = 31;
  258.  
  259.         if (B1 <= B2)
  260.             for (i = B1; ; i++) {
  261.                 xchr.A[i] = '?';
  262.                 if (i == B2) break;
  263.             }
  264.     }
  265.     xchr.A[32] = ' ';
  266.     xchr.A[33] = '!';
  267.     xchr.A[34] = '"';
  268.     xchr.A[35] = '#';
  269.     xchr.A[36] = '$';
  270.     xchr.A[37] = '%';
  271.     xchr.A[38] = '&';
  272.     xchr.A[39] = '\'';
  273.     xchr.A[40] = '(';
  274.     xchr.A[41] = ')';
  275.     xchr.A[42] = '*';
  276.     xchr.A[43] = '+';
  277.     xchr.A[44] = ',';
  278.     xchr.A[45] = '-';
  279.     xchr.A[46] = '.';
  280.     xchr.A[47] = '/';
  281.     xchr.A[48] = '0';
  282.     xchr.A[49] = '1';
  283.     xchr.A[50] = '2';
  284.     xchr.A[51] = '3';
  285.     xchr.A[52] = '4';
  286.     xchr.A[53] = '5';
  287.     xchr.A[54] = '6';
  288.     xchr.A[55] = '7';
  289.     xchr.A[56] = '8';
  290.     xchr.A[57] = '9';
  291.     xchr.A[58] = ':';
  292.     xchr.A[59] = ';';
  293.     xchr.A[60] = '<';
  294.     xchr.A[61] = '=';
  295.     xchr.A[62] = '>';
  296.     xchr.A[63] = '?';
  297.     xchr.A[64] = '@';
  298.     xchr.A[65] = 'A';
  299.     xchr.A[66] = 'B';
  300.     xchr.A[67] = 'C';
  301.     xchr.A[68] = 'D';
  302.     xchr.A[69] = 'E';
  303.     xchr.A[70] = 'F';
  304.     xchr.A[71] = 'G';
  305.     xchr.A[72] = 'H';
  306.     xchr.A[73] = 'I';
  307.     xchr.A[74] = 'J';
  308.     xchr.A[75] = 'K';
  309.     xchr.A[76] = 'L';
  310.     xchr.A[77] = 'M';
  311.     xchr.A[78] = 'N';
  312.     xchr.A[79] = 'O';
  313.     xchr.A[80] = 'P';
  314.     xchr.A[81] = 'Q';
  315.     xchr.A[82] = 'R';
  316.     xchr.A[83] = 'S';
  317.     xchr.A[84] = 'T';
  318.     xchr.A[85] = 'U';
  319.     xchr.A[86] = 'V';
  320.     xchr.A[87] = 'W';
  321.     xchr.A[88] = 'X';
  322.     xchr.A[89] = 'Y';
  323.     xchr.A[90] = 'Z';
  324.     xchr.A[91] = '[';
  325.     xchr.A[92] = '\\';
  326.     xchr.A[93] = ']';
  327.     xchr.A[94] = '^';
  328.     xchr.A[95] = '_';
  329.     xchr.A[96] = '`';
  330.     xchr.A[97] = 'a';
  331.     xchr.A[98] = 'b';
  332.     xchr.A[99] = 'c';
  333.     xchr.A[100] = 'd';
  334.     xchr.A[101] = 'e';
  335.     xchr.A[102] = 'f';
  336.     xchr.A[103] = 'g';
  337.     xchr.A[104] = 'h';
  338.     xchr.A[105] = 'i';
  339.     xchr.A[106] = 'j';
  340.     xchr.A[107] = 'k';
  341.     xchr.A[108] = 'l';
  342.     xchr.A[109] = 'm';
  343.     xchr.A[110] = 'n';
  344.     xchr.A[111] = 'o';
  345.     xchr.A[112] = 'p';
  346.     xchr.A[113] = 'q';
  347.     xchr.A[114] = 'r';
  348.     xchr.A[115] = 's';
  349.     xchr.A[116] = 't';
  350.     xchr.A[117] = 'u';
  351.     xchr.A[118] = 'v';
  352.     xchr.A[119] = 'w';
  353.     xchr.A[120] = 'x';
  354.     xchr.A[121] = 'y';
  355.     xchr.A[122] = 'z';
  356.     xchr.A[123] = '{';
  357.     xchr.A[124] = '|';
  358.     xchr.A[125] = '}';
  359.     xchr.A[126] = '~';
  360.     {
  361.         integer    B3 = 127,
  362.             B4 = 255;
  363.  
  364.         if (B3 <= B4)
  365.             for (i = B3; ; i++) {
  366.                 xchr.A[i] = '?';
  367.                 if (i == B4) break;
  368.             }
  369.     }
  370.     {
  371.         integer    B5 = 0,
  372.             B6 = 127;
  373.  
  374.         if (B5 <= B6)
  375.             for (i = B5; ; i++) {
  376.                 xord.A[i] = 32;
  377.                 if (i == B6) break;
  378.             }
  379.     }
  380.     {
  381.         integer    B7 = 32,
  382.             B8 = 126;
  383.  
  384.         if (B7 <= B8)
  385.             for (i = B7; ; i++) {
  386.                 xord.A[xchr.A[i]] = i;
  387.                 if (i == B8) break;
  388.             }
  389.     }
  390.     nf = 0;
  391.     widthptr = 0;
  392.     fontname.A[0] = 0;
  393.     fontspace.A[0] = 0;
  394.     maxpages = 1000000;
  395.     startvals = 0;
  396.     startthere.A[0] = false;
  397.     inpostamble = false;
  398.     (void)strncpy(defaultdirectory.A, "TeXfonts:", sizeof(defaultdirectory.A));
  399.     maxv = 2147483548;
  400.     maxh = 2147483548;
  401.     maxs = stacksize + 1;
  402.     maxvsofar = 0;
  403.     maxhsofar = 0;
  404.     maxssofar = 0;
  405.     pagecount = 0;
  406.     oldbackpointer = -1;
  407.     started = false;
  408. }
  409.  
  410.  void
  411. jumpout()
  412. {
  413.     longjmp(J[0].jb, 9999);
  414. }
  415.  
  416.  void
  417. opentfmfile()
  418. {
  419.     if (testaccess(4, 3))
  420.         Resetx(tfmfile, realnameoffile.A);
  421.     else {
  422.         (void)fprintf(errfile.fp, "TFM file not found\n"), Putl(errfile, 1);
  423.         longjmp(J[0].jb, 30);
  424.     }
  425. }
  426.  
  427.  void
  428. readtfmword()
  429. {
  430.     char    byte;
  431.  
  432.     byte = Getchr(tfmfile);
  433.     b0 = (unsigned)(byte);
  434.     if (b0 < 0)
  435.         b0 = b0 + 256;
  436.     byte = Getchr(tfmfile);
  437.     b1 = (unsigned)(byte);
  438.     if (b1 < 0)
  439.         b1 = b1 + 256;
  440.     byte = Getchr(tfmfile);
  441.     b2 = (unsigned)(byte);
  442.     if (b2 < 0)
  443.         b2 = b2 + 256;
  444.     byte = Getchr(tfmfile);
  445.     b3 = (unsigned)(byte);
  446.     if (b3 < 0)
  447.         b3 = b3 + 256;
  448. }
  449.  
  450.  integer
  451. dvilength()
  452. {
  453.     register integer    R93;
  454.  
  455.     setpos(&dvifile, -1);
  456.     R93 = curpos(&dvifile);
  457.     return R93;
  458. }
  459.  
  460.  void
  461. movetobyte(n)
  462.     integer    n;
  463. {
  464.     setpos(&dvifile, n);
  465.     curloc = n;
  466. }
  467.  
  468.  void
  469. printfont(f)
  470.     integer    f;
  471. {
  472.     register unsigned short    k;
  473.  
  474.     if (f == nf)
  475.         (void)fprintf(errfile.fp, "UNDEFINED!"), Putl(errfile, 0);
  476.     else {
  477.         {
  478.             unsigned short    B9 = fontname.A[f],
  479.                 B10 = fontname.A[f + 1] - 1;
  480.  
  481.             if (B9 <= B10)
  482.                 for (k = B9; ; k++) {
  483.                     Putchr(xchr.A[names.A[k]], errfile);
  484.                     if (k == B10) break;
  485.                 }
  486.         }
  487.     }
  488. }
  489.  
  490.  boolean
  491. intfm(z)
  492.     integer    z;
  493. {
  494.     register boolean    R94;
  495.     register integer    k;
  496.     integer    lh;
  497.     integer    nw;
  498.     unsigned short    wp;
  499.     integer    alpha, beta;
  500.  
  501.     readtfmword();
  502.     lh = b2 * 256 + b3;
  503.     readtfmword();
  504.     fontbc.A[nf] = b0 * 256 + b1;
  505.     fontec.A[nf] = b2 * 256 + b3;
  506.     if (fontec.A[nf] < fontbc.A[nf])
  507.         fontbc.A[nf] = fontec.A[nf] + 1;
  508.     if (widthptr + fontec.A[nf] - fontbc.A[nf] + 1 > maxwidths) {
  509.         (void)fprintf(errfile.fp, "---not loaded, DVIDOC needs larger width table\n"), Putl(errfile, 1);
  510.         goto L9998;
  511.     }
  512.     wp = widthptr + fontec.A[nf] - fontbc.A[nf] + 1;
  513.     readtfmword();
  514.     nw = b0 * 256 + b1;
  515.     if ((nw == 0) || (nw > 256))
  516.         goto L9997;
  517.     {
  518.         integer    B11 = 1,
  519.             B12 = 3 + lh;
  520.  
  521.         if (B11 <= B12)
  522.             for (k = B11; ; k++) {
  523.                 if (eofdvifile)
  524.                     goto L9997;
  525.                 readtfmword();
  526.                 if (k == 4)
  527.                     if (b0 < 128)
  528.                         tfmchecksum = ((b0 * 256 + b1) * 256 + b2) * 256 + b3;
  529.                     else
  530.                         tfmchecksum = (((b0 - 256) * 256 + b1) * 256 + b2) * 256 + b3;
  531.                 if (k == B12) break;
  532.             }
  533.     }
  534.     if (wp > 0)
  535.         {
  536.             integer    B13 = widthptr,
  537.                 B14 = wp - 1;
  538.  
  539.             if (B13 <= B14)
  540.                 for (k = B13; ; k++) {
  541.                     readtfmword();
  542.                     if (b0 > nw)
  543.                         goto L9997;
  544.                     width.A[k] = b0;
  545.                     if (k == B14) break;
  546.                 }
  547.         }
  548.     {
  549.         alpha = 16 * z;
  550.         beta = 16;
  551.         while (z >= 8388608) {
  552.             z = z / 2;
  553.             beta = beta / 2;
  554.         }
  555.     }
  556.     {
  557.         integer    B15 = 0,
  558.             B16 = nw - 1;
  559.  
  560.         if (B15 <= B16)
  561.             for (k = B15; ; k++) {
  562.                 readtfmword();
  563.                 inwidth.A[k] = (((((b3 * z) / 256) + (b2 * z)) / 256) + (b1 * z)) / beta;
  564.                 if (b0 > 0)
  565.                     if (b0 < 255)
  566.                         goto L9997;
  567.                     else
  568.                         inwidth.A[k] = inwidth.A[k] - alpha;
  569.                 if (k == B16) break;
  570.             }
  571.     }
  572.     widthbase.A[nf] = widthptr - fontbc.A[nf];
  573.     if (wp > 0)
  574.         {
  575.             integer    B17 = widthptr,
  576.                 B18 = wp - 1;
  577.  
  578.             if (B17 <= B18)
  579.                 for (k = B17; ; k++) {
  580.                     width.A[k] = inwidth.A[width.A[k]];
  581.                     pixelwidth.A[k] = Round(horizconv * (width.A[k]));
  582.                     if (k == B18) break;
  583.                 }
  584.         }
  585.     widthptr = wp;
  586.     R94 = true;
  587.     goto L9999;
  588. L9997:
  589.     (void)fprintf(errfile.fp, "---not loaded, TFM file is bad\n"), Putl(errfile, 1);
  590. L9998:
  591.     R94 = false;
  592. L9999:
  593.     ;
  594.     return R94;
  595. }
  596.  
  597.  boolean
  598. startmatch()
  599. {
  600.     register boolean    R95;
  601.     register unsigned char    k;
  602.     boolean    match;
  603.  
  604.     match = true;
  605.     {
  606.         unsigned char    B19 = 0,
  607.             B20 = startvals;
  608.  
  609.         if (B19 <= B20)
  610.             for (k = B19; ; k++) {
  611.                 if (startthere.A[k] && (startcount.A[k] != count.A[k]))
  612.                     match = false;
  613.                 if (k == B20) break;
  614.             }
  615.     }
  616.     R95 = match;
  617.     return R95;
  618. }
  619.  
  620.  void
  621. copyln()
  622. {
  623.     unsigned char    k;
  624.  
  625.     k = 2;
  626.     while ((k < namelength) && (curname.A[k - 1] != ' ')) {
  627.         buffer.A[k - 3] = xord.A[curname.A[k - 1]];
  628.         k = k + 1;
  629.     }
  630.     buffer.A[k - 3] = xord.A[' '];
  631. }
  632.  
  633.  integer
  634. getinteger()
  635. {
  636.     register integer    R96;
  637.     integer    x;
  638.     boolean    negative;
  639.  
  640.     if (buffer.A[bufptr] == 45) {
  641.         negative = true;
  642.         bufptr = bufptr + 1;
  643.     } else
  644.         negative = false;
  645.     x = 0;
  646.     while ((buffer.A[bufptr] >= 48) && (buffer.A[bufptr] <= 57)) {
  647.         x = 10 * x + buffer.A[bufptr] - 48;
  648.         bufptr = bufptr + 1;
  649.     }
  650.     if (negative)
  651.         R96 = -x;
  652.     else
  653.         R96 = x;
  654.     return R96;
  655. }
  656.  
  657.  void
  658. options()
  659. {
  660.     integer    j, k;
  661.  
  662.     logerrors = false;
  663.     startvals = 0;
  664.     startthere.A[0] = false;
  665.     maxpages = 1000000;
  666.     horizresolution = 13.76582;
  667.     vertresolution = 6.0225;
  668.     newmag = 0;
  669.     basename.A[1 - 1] = ' ';
  670.     j = 1;
  671.     while (j < argc) {
  672.         Argvgt(j, curname.A, sizeof(curname.A));
  673.         if (curname.A[1 - 1] == '-') {
  674.             if (curname.A[2 - 1] == 'e')
  675.                 logerrors = true;
  676.             else
  677.                 if (curname.A[2 - 1] == 's') {
  678.                     copyln();
  679.                     bufptr = 0;
  680.                     k = 0;
  681.                     if (buffer.A[0] != 32)
  682.                         do {
  683.                             if (buffer.A[bufptr] == 42) {
  684.                                 startthere.A[k] = false;
  685.                                 bufptr = bufptr + 1;
  686.                             } else {
  687.                                 startthere.A[k] = true;
  688.                                 startcount.A[k] = getinteger();
  689.                             }
  690.                             if ((k < 9) && (buffer.A[bufptr] == 46)) {
  691.                                 k = k + 1;
  692.                                 bufptr = bufptr + 1;
  693.                             } else
  694.                                 if (buffer.A[bufptr] == 32)
  695.                                     startvals = k;
  696.                                 else {
  697.                                     (void)fprintf(stderr, "1.*.-5 specifies first page with \\count0=1, \\count2=-5.\n"), Putl(output, 1);
  698.                                     longjmp(J[0].jb, 30);
  699.                                 }
  700.                         } while (!(startvals == k));
  701.                 } else
  702.                     if (curname.A[2 - 1] == 'm') {
  703.                         copyln();
  704.                         bufptr = 0;
  705.                         if (buffer.A[0] != 32) {
  706.                             maxpages = getinteger();
  707.                             if (maxpages <= 0) {
  708.                                 (void)fprintf(stderr, "Maxpages should be a positive number.\n"), Putl(output, 1);
  709.                                 longjmp(J[0].jb, 30);
  710.                             }
  711.                         }
  712.                     } else {
  713.                         (void)fprintf(stderr, "Usage: dvidoc [-e] [-s<start page spec>] [-m<maxpages>] <dvi-file>\n"), Putl(output, 1);
  714.                         longjmp(J[0].jb, 30);
  715.                     }
  716.         } else
  717.             if (basename.A[1 - 1] == ' ')
  718.                 Argvgt(j, basename.A, sizeof(basename.A));
  719.             else {
  720.                 (void)fprintf(stderr, "Only one input file please\n"), Putl(output, 1);
  721.                 longjmp(J[0].jb, 30);
  722.             }
  723.         j = j + 1;
  724.     }
  725.     if (basename.A[1 - 1] == ' ') {
  726.         (void)fprintf(stderr, "No input file specified\n"), Putl(output, 1);
  727.         longjmp(J[0].jb, 30);
  728.     }
  729. }
  730.  
  731.  void
  732. definefont(e)
  733.     integer    e;
  734. {
  735.     unsigned char    f;
  736.     integer    p;
  737.     integer    n;
  738.     integer    c, q, d;
  739.     unsigned char    r;
  740.     unsigned short    j;
  741.     register unsigned short    k;
  742.     boolean    mismatch;
  743.  
  744.     if (nf == maxfonts) {
  745.         (void)fprintf(stderr, " DVIDOC capacity exceeded (max fonts=%1d)!\n", maxfonts), Putl(output, 1);
  746.         jumpout();
  747.     }
  748.     fontnum.A[nf] = e;
  749.     f = 0;
  750.     while (fontnum.A[f] != e)
  751.         f = f + 1;
  752.     c = signedquad();
  753.     fontchecksum.A[nf] = c;
  754.     q = signedquad();
  755.     fontscaledsize.A[nf] = q;
  756.     d = signedquad();
  757.     fontdesignsize.A[nf] = d;
  758.     p = getbyte();
  759.     n = getbyte();
  760.     if (fontname.A[nf] + n + p > namesize) {
  761.         (void)fprintf(stderr, " DVIDOC capacity exceeded (name size=%1d)!\n", namesize), Putl(output, 1);
  762.         jumpout();
  763.     }
  764.     fontname.A[nf + 1] = fontname.A[nf] + n + p;
  765.     (void)fprintf(errfile.fp, "Font %1d: ", e), Putl(errfile, 0);
  766.     if (n + p == 0)
  767.         (void)fprintf(errfile.fp, "null font name!"), Putl(errfile, 0);
  768.     else
  769.         {
  770.             unsigned short    B21 = fontname.A[nf],
  771.                 B22 = fontname.A[nf + 1] - 1;
  772.  
  773.             if (B21 <= B22)
  774.                 for (k = B21; ; k++) {
  775.                     names.A[k] = getbyte();
  776.                     if (k == B22) break;
  777.                 }
  778.         }
  779.     nf = nf + 1;
  780.     printfont(nf - 1);
  781.     nf = nf - 1;
  782.     if (inpostamble) {
  783.         if (f < nf)
  784.             (void)fprintf(errfile.fp, "---this font was already defined!\n"), Putl(errfile, 1);
  785.     } else {
  786.         if (f == nf)
  787.             (void)fprintf(errfile.fp, "---this font wasn't loaded before!\n"), Putl(errfile, 1);
  788.     }
  789.     if (f == nf) {
  790.         {
  791.             unsigned short    B23 = 1,
  792.                 B24 = namelength;
  793.  
  794.             if (B23 <= B24)
  795.                 for (k = B23; ; k++) {
  796.                     curname.A[k - 1] = ' ';
  797.                     if (k == B24) break;
  798.                 }
  799.         }
  800.         r = 0;
  801.         {
  802.             unsigned short    B25 = fontname.A[nf],
  803.                 B26 = fontname.A[nf + 1] - 1;
  804.  
  805.             if (B25 <= B26)
  806.                 for (k = B25; ; k++) {
  807.                     r = r + 1;
  808.                     if (r + 4 > namelength) {
  809.                         (void)fprintf(stderr, " DVIDOC capacity exceeded (max font name length=%1d)!\n", namelength), Putl(output, 1);
  810.                         jumpout();
  811.                     }
  812.                     curname.A[r - 1] = xchr.A[names.A[k]];
  813.                     if (k == B26) break;
  814.                 }
  815.         }
  816.         curname.A[r + 1 - 1] = '.';
  817.         curname.A[r + 2 - 1] = 't';
  818.         curname.A[r + 3 - 1] = 'f';
  819.         curname.A[r + 4 - 1] = 'm';
  820.         opentfmfile();
  821.         if (eofdvifile)
  822.             (void)fprintf(errfile.fp, "---not loaded, TFM file can't be opened!"), Putl(errfile, 0);
  823.         else {
  824.             if ((q <= 0) || (q >= 134217728))
  825.                 (void)fprintf(errfile.fp, "---not loaded, bad scale (%1d)!", q), Putl(errfile, 0);
  826.             else
  827.                 if ((d <= 0) || (d >= 134217728))
  828.                     (void)fprintf(errfile.fp, "---not loaded, bad design size (%1d)!", d), Putl(errfile, 0);
  829.                 else
  830.                     if (intfm(q)) {
  831.                         fontspace.A[nf] = q / 6;
  832.                         if ((c != 0) && (tfmchecksum != 0) && (c != tfmchecksum)) {
  833.                             (void)fprintf(errfile.fp, "---beware: check sums do not agree!\n"), Putl(errfile, 1);
  834.                             (void)fprintf(errfile.fp, "   (%1d vs. %1d)\n", c, tfmchecksum), Putl(errfile, 1);
  835.                             (void)fprintf(errfile.fp, "   "), Putl(errfile, 0);
  836.                         }
  837.                         (void)fprintf(errfile.fp, "---loaded at size %1d DVI units", q), Putl(errfile, 0);
  838.                         d = Round((100.0 * horizconv * q) / ((real)(truehorizconv * d)));
  839.                         if (d != 100) {
  840.                             Putchr(' ', errfile),Putchr('\n', errfile);
  841.                             (void)fprintf(errfile.fp, " (this font is magnified %1d%%)", d), Putl(errfile, 0);
  842.                         }
  843.                         nf = nf + 1;
  844.                         fontspace.A[nf] = 0;
  845.                     }
  846.         }
  847.         Putchr(' ', errfile),Putchr('\n', errfile);
  848.     } else {
  849.         if (fontchecksum.A[f] != c)
  850.             (void)fprintf(errfile.fp, "---check sum doesn't match previous definition!\n"), Putl(errfile, 1);
  851.         if (fontscaledsize.A[f] != q)
  852.             (void)fprintf(errfile.fp, "---scaled size doesn't match previous definition!\n"), Putl(errfile, 1);
  853.         if (fontdesignsize.A[f] != d)
  854.             (void)fprintf(errfile.fp, "---design size doesn't match previous definition!\n"), Putl(errfile, 1);
  855.         j = fontname.A[f];
  856.         k = fontname.A[nf];
  857.         mismatch = false;
  858.         while (j < fontname.A[f + 1]) {
  859.             if (names.A[j] != names.A[k])
  860.                 mismatch = true;
  861.             j = j + 1;
  862.             k = k + 1;
  863.         }
  864.         if (k != fontname.A[nf + 1])
  865.             mismatch = true;
  866.         if (mismatch)
  867.             (void)fprintf(errfile.fp, "---font name doesn't match previous definition!\n"), Putl(errfile, 1);
  868.         Putchr(' ', errfile),Putchr('\n', errfile);
  869.     }
  870. }
  871.  
  872.  void
  873. flushpage()
  874. {
  875.     register unsigned char    i;
  876.     register unsigned char    j;
  877.  
  878.     {
  879.         unsigned char    B27 = 1,
  880.             B28 = pagehwm;
  881.  
  882.         if (B27 <= B28)
  883.             for (j = B27; ; j++) {
  884.                 {
  885.                     unsigned char    B29 = 1,
  886.                         B30 = linehwm.A[j - 1];
  887.  
  888.                     if (B29 <= B30)
  889.                         for (i = B29; ; i++) {
  890.                             Putchr(xchr.A[pagebuffer.A[i - 1].A[j - 1]], output);
  891.                             if (i == B30) break;
  892.                         }
  893.                 }
  894.                 Putchr('\n', output);
  895.                 if (j == B28) break;
  896.             }
  897.     }
  898.     Putchr(12, output);
  899. }
  900.  
  901.  void
  902. emptypage()
  903. {
  904.     pagehwm = 0;
  905. }
  906.  
  907.  void
  908. outchar(p, hh, vv)
  909.     integer    p, hh, vv;
  910. {
  911.     unsigned char    i;
  912.     unsigned char    j;
  913.     register integer    k;
  914.     asciicode    c;
  915.  
  916.     if ((p > 32) && (p <= 126))
  917.         c = p;
  918.     else
  919.         c = xord.A['?'];
  920.     if ((hh > pagewidthmax - 1) || (vv > pagelengthmax - 1) ||
  921.             (hh < 0) || (vv < 0)) {
  922.         Putchr(' ', errfile),Putchr('\n', errfile);
  923.         (void)fprintf(errfile.fp, "Character \"%c\" set at column %1d", xchr.A[c], hh + 1), Putl(errfile, 0);
  924.         (void)fprintf(errfile.fp, " and row %1d,\n", vv + 1), Putl(errfile, 1);
  925.         (void)fprintf(errfile.fp, "outside the range of DVIDOC ("), Putl(errfile, 0);
  926.         (void)fprintf(errfile.fp, "%1d,%1d).", pagewidthmax, pagelengthmax), Putl(errfile, 0);
  927.         Putchr(' ', errfile),Putchr('\n', errfile);
  928.     } else {
  929.         i = hh + 1;
  930.         j = vv + 1;
  931.         if (j > pagehwm) {
  932.             {
  933.                 integer    B31 = pagehwm + 1,
  934.                     B32 = j;
  935.  
  936.                 if (B31 <= B32)
  937.                     for (k = B31; ; k++) {
  938.                         linehwm.A[k - 1] = 0;
  939.                         if (k == B32) break;
  940.                     }
  941.             }
  942.             pagehwm = j;
  943.         }
  944.         if (i > linehwm.A[j - 1]) {
  945.             {
  946.                 integer    B33 = linehwm.A[j - 1] + 1,
  947.                     B34 = i;
  948.  
  949.                 if (B33 <= B34)
  950.                     for (k = B33; ; k++) {
  951.                         pagebuffer.A[k - 1].A[j - 1] = xord.A[' '];
  952.                         if (k == B34) break;
  953.                     }
  954.             }
  955.             linehwm.A[j - 1] = i;
  956.         }
  957.         pagebuffer.A[i - 1].A[j - 1] = c;
  958.     }
  959. }
  960.  
  961.  integer
  962. firstpar(o)
  963.     eightbits    o;
  964. {
  965.     register integer    R97;
  966.  
  967.     switch (o) {
  968.       case 0:  case 1:  case 2:  case 3:
  969.       case 4:  case 5:  case 6:  case 7:
  970.       case 8:  case 9:  case 10:  case 11:
  971.       case 12:  case 13:  case 14:  case 15:
  972.       case 16:  case 17:  case 18:  case 19:
  973.       case 20:  case 21:  case 22:  case 23:
  974.       case 24:  case 25:  case 26:  case 27:
  975.       case 28:  case 29:  case 30:  case 31:
  976.       case 32:  case 33:  case 34:  case 35:
  977.       case 36:  case 37:  case 38:  case 39:
  978.       case 40:  case 41:  case 42:  case 43:
  979.       case 44:  case 45:  case 46:  case 47:
  980.       case 48:  case 49:  case 50:  case 51:
  981.       case 52:  case 53:  case 54:  case 55:
  982.       case 56:  case 57:  case 58:  case 59:
  983.       case 60:  case 61:  case 62:  case 63:
  984.       case 64:  case 65:  case 66:  case 67:
  985.       case 68:  case 69:  case 70:  case 71:
  986.       case 72:  case 73:  case 74:  case 75:
  987.       case 76:  case 77:  case 78:  case 79:
  988.       case 80:  case 81:  case 82:  case 83:
  989.       case 84:  case 85:  case 86:  case 87:
  990.       case 88:  case 89:  case 90:  case 91:
  991.       case 92:  case 93:  case 94:  case 95:
  992.       case 96:  case 97:  case 98:  case 99:
  993.       case 100:  case 101:  case 102:  case 103:
  994.       case 104:  case 105:  case 106:  case 107:
  995.       case 108:  case 109:  case 110:  case 111:
  996.       case 112:  case 113:  case 114:  case 115:
  997.       case 116:  case 117:  case 118:  case 119:
  998.       case 120:  case 121:  case 122:  case 123:
  999.       case 124:  case 125:  case 126:  case 127:
  1000.         R97 = o - 0;
  1001.         break ;
  1002.       case 128:  case 133:  case 235:  case 239:
  1003.       case 243:
  1004.         R97 = getbyte();
  1005.         break ;
  1006.       case 129:  case 134:  case 236:  case 240:
  1007.       case 244:
  1008.         R97 = gettwobytes();
  1009.         break ;
  1010.       case 130:  case 135:  case 237:  case 241:
  1011.       case 245:
  1012.         R97 = getthreebytes();
  1013.         break ;
  1014.       case 143:  case 148:  case 153:  case 157:
  1015.       case 162:  case 167:
  1016.         R97 = signedbyte();
  1017.         break ;
  1018.       case 144:  case 149:  case 154:  case 158:
  1019.       case 163:  case 168:
  1020.         R97 = signedpair();
  1021.         break ;
  1022.       case 145:  case 150:  case 155:  case 159:
  1023.       case 164:  case 169:
  1024.         R97 = signedtrio();
  1025.         break ;
  1026.       case 131:  case 132:  case 136:  case 137:
  1027.       case 146:  case 151:  case 156:  case 160:
  1028.       case 165:  case 170:  case 238:  case 242:
  1029.       case 246:
  1030.         R97 = signedquad();
  1031.         break ;
  1032.       case 138:  case 139:  case 140:  case 141:
  1033.       case 142:  case 247:  case 248:  case 249:
  1034.       case 250:  case 251:  case 252:  case 253:
  1035.       case 254:  case 255:
  1036.         R97 = 0;
  1037.         break ;
  1038.       case 147:
  1039.         R97 = w;
  1040.         break ;
  1041.       case 152:
  1042.         R97 = x;
  1043.         break ;
  1044.       case 161:
  1045.         R97 = y;
  1046.         break ;
  1047.       case 166:
  1048.         R97 = z;
  1049.         break ;
  1050.       case 171:  case 172:  case 173:  case 174:
  1051.       case 175:  case 176:  case 177:  case 178:
  1052.       case 179:  case 180:  case 181:  case 182:
  1053.       case 183:  case 184:  case 185:  case 186:
  1054.       case 187:  case 188:  case 189:  case 190:
  1055.       case 191:  case 192:  case 193:  case 194:
  1056.       case 195:  case 196:  case 197:  case 198:
  1057.       case 199:  case 200:  case 201:  case 202:
  1058.       case 203:  case 204:  case 205:  case 206:
  1059.       case 207:  case 208:  case 209:  case 210:
  1060.       case 211:  case 212:  case 213:  case 214:
  1061.       case 215:  case 216:  case 217:  case 218:
  1062.       case 219:  case 220:  case 221:  case 222:
  1063.       case 223:  case 224:  case 225:  case 226:
  1064.       case 227:  case 228:  case 229:  case 230:
  1065.       case 231:  case 232:  case 233:  case 234:
  1066.         R97 = o - 171;
  1067.         break ;
  1068.       default:
  1069.         Caseerror(Line);
  1070.     }
  1071.     return R97;
  1072. }
  1073.  
  1074.  integer
  1075. horizrulepixels(x)
  1076.     integer    x;
  1077. {
  1078.     register integer    R98;
  1079.     integer    n;
  1080.  
  1081.     n = Trunc(horizconv * x);
  1082.     if (n < horizconv * x)
  1083.         R98 = n + 1;
  1084.     else
  1085.         R98 = n;
  1086.     return R98;
  1087. }
  1088.  
  1089.  integer
  1090. vertrulepixels(x)
  1091.     integer    x;
  1092. {
  1093.     register integer    R99;
  1094.     integer    n;
  1095.  
  1096.     n = Trunc(vertconv * x);
  1097.     if (n < vertconv * x)
  1098.         R99 = n + 1;
  1099.     else
  1100.         R99 = n;
  1101.     return R99;
  1102. }
  1103.  
  1104.  boolean
  1105. specialcases(o, p, a)
  1106.     eightbits    o;
  1107.     integer    p, a;
  1108. {
  1109.     register boolean    R100;
  1110.     integer    q;
  1111.     register integer    k;
  1112.     boolean    badchar;
  1113.     boolean    pure;
  1114.     integer    vvv;
  1115.  
  1116.     pure = true;
  1117.     switch (o) {
  1118.       case 133:  case 134:  case 135:  case 136:
  1119.         goto L30;
  1120.         break ;
  1121.       case 157:  case 158:  case 159:  case 160:
  1122.         if (abs(p) >= 5 * fontspace.A[curfont])
  1123.             vv = Round(vertconv * (v + p));
  1124.         else
  1125.             vv = vv + Round(vertconv * (p));
  1126.         goto L44;
  1127.         break ;
  1128.       case 161:  case 162:  case 163:  case 164:
  1129.       case 165:
  1130.         y = p;
  1131.         if (abs(p) >= 5 * fontspace.A[curfont])
  1132.             vv = Round(vertconv * (v + p));
  1133.         else
  1134.             vv = vv + Round(vertconv * (p));
  1135.         goto L44;
  1136.         break ;
  1137.       case 166:  case 167:  case 168:  case 169:
  1138.       case 170:
  1139.         z = p;
  1140.         if (abs(p) >= 5 * fontspace.A[curfont])
  1141.             vv = Round(vertconv * (v + p));
  1142.         else
  1143.             vv = vv + Round(vertconv * (p));
  1144.         goto L44;
  1145.         break ;
  1146.       case 171:  case 172:  case 173:  case 174:
  1147.       case 175:  case 176:  case 177:  case 178:
  1148.       case 179:  case 180:  case 181:  case 182:
  1149.       case 183:  case 184:  case 185:  case 186:
  1150.       case 187:  case 188:  case 189:  case 190:
  1151.       case 191:  case 192:  case 193:  case 194:
  1152.       case 195:  case 196:  case 197:  case 198:
  1153.       case 199:  case 200:  case 201:  case 202:
  1154.       case 203:  case 204:  case 205:  case 206:
  1155.       case 207:  case 208:  case 209:  case 210:
  1156.       case 211:  case 212:  case 213:  case 214:
  1157.       case 215:  case 216:  case 217:  case 218:
  1158.       case 219:  case 220:  case 221:  case 222:
  1159.       case 223:  case 224:  case 225:  case 226:
  1160.       case 227:  case 228:  case 229:  case 230:
  1161.       case 231:  case 232:  case 233:  case 234:
  1162.         goto L46;
  1163.         break ;
  1164.       case 235:  case 236:  case 237:  case 238:
  1165.         goto L46;
  1166.         break ;
  1167.       case 243:  case 244:  case 245:  case 246:
  1168.         definefont(p);
  1169.         goto L30;
  1170.         break ;
  1171.       case 239:  case 240:  case 241:  case 242:
  1172.         (void)fprintf(errfile.fp, "xxx'"), Putl(errfile, 0);
  1173.         badchar = false;
  1174.         {
  1175.             integer    B35 = 1,
  1176.                 B36 = p;
  1177.  
  1178.             if (B35 <= B36)
  1179.                 for (k = B35; ; k++) {
  1180.                     q = getbyte();
  1181.                     if ((q < 32) || (q > 126))
  1182.                         badchar = true;
  1183.                     Putchr(xchr.A[q], errfile);
  1184.                     if (k == B36) break;
  1185.                 }
  1186.         }
  1187.         Putchr('\'', errfile);
  1188.         if (badchar)
  1189.             (void)fprintf(errfile.fp, "non-ASCII character in xxx command!\n"), Putl(errfile, 1);
  1190.         goto L30;
  1191.         break ;
  1192.       case 247:
  1193.         (void)fprintf(errfile.fp, "preamble command within a page!\n"), Putl(errfile, 1);
  1194.         goto L9998;
  1195.         break ;
  1196.       case 248:  case 249:
  1197.         (void)fprintf(errfile.fp, "postamble command within a page!\n"), Putl(errfile, 1);
  1198.         goto L9998;
  1199.         break ;
  1200.       default:
  1201. {
  1202.             (void)fprintf(errfile.fp, "undefined command %1d!\n", o), Putl(errfile, 1);
  1203.             goto L30;
  1204.         }
  1205.     }
  1206. L44:
  1207.     if ((v > 0) && (p > 0))
  1208.         if (v > 2147483647 - p) {
  1209.             (void)fprintf(errfile.fp, "arithmetic overflow! parameter changed from %1d to %1d\n", p, 2147483647 - v), Putl(errfile, 1);
  1210.             p = 2147483647 - v;
  1211.         }
  1212.     if ((v < 0) && (p < 0))
  1213.         if (-v > p + 2147483647) {
  1214.             (void)fprintf(errfile.fp, "arithmetic overflow! parameter changed from %1d to %1d\n", p, (-v) - 2147483647), Putl(errfile, 1);
  1215.             p = (-v) - 2147483647;
  1216.         }
  1217.     vvv = Round(vertconv * (v + p));
  1218.     if (abs(vvv - vv) > 2)
  1219.         if (vvv > vv)
  1220.             vv = vvv - 2;
  1221.         else
  1222.             vv = vvv + 2;
  1223.     v = v + p;
  1224.     if (abs(v) > maxvsofar) {
  1225.         if (abs(v) > maxv + 99) {
  1226.             (void)fprintf(errfile.fp, "warning: |v|>%1d!\n", maxv), Putl(errfile, 1);
  1227.             maxv = abs(v);
  1228.         }
  1229.         maxvsofar = abs(v);
  1230.     }
  1231.     goto L30;
  1232. L46:
  1233.     fontnum.A[nf] = p;
  1234.     curfont = 0;
  1235.     while (fontnum.A[curfont] != p)
  1236.         curfont = curfont + 1;
  1237.     goto L30;
  1238. L9998:
  1239.     pure = false;
  1240. L30:
  1241.     R100 = pure;
  1242.     return R100;
  1243. }
  1244.  
  1245.  boolean
  1246. dopage()
  1247. {
  1248.     register boolean    R101;
  1249.     eightbits    o;
  1250.     integer    p, q;
  1251.     integer    a;
  1252.     register integer    j;
  1253.     register integer    i;
  1254.     integer    hhh;
  1255.  
  1256.     emptypage();
  1257.     curfont = nf;
  1258.     s = 0;
  1259.     h = 0;
  1260.     v = 0;
  1261.     w = 0;
  1262.     x = 0;
  1263.     y = 0;
  1264.     z = 0;
  1265.     hh = 0;
  1266.     vv = 0;
  1267.     while (true) {
  1268.         a = curloc;
  1269.         o = getbyte();
  1270.         p = firstpar(o);
  1271.         if (eofdvifile) {
  1272.             (void)fprintf(stderr, " Bad DVI file: the file ended prematurely!\n"), Putl(output, 1);
  1273.             jumpout();
  1274.         }
  1275.         if (o < 128) {
  1276.             outchar(p, hh, vv);
  1277.         } else
  1278.             switch (o) {
  1279.               case 128:  case 129:  case 130:  case 131:
  1280.                 outchar(p, hh, vv);
  1281.                 goto L41;
  1282.                 break ;
  1283.               case 132:
  1284.                 goto L42;
  1285.                 break ;
  1286.               case 137:
  1287.                 goto L42;
  1288.                 break ;
  1289.               case 138:
  1290.                 goto L30;
  1291.                 break ;
  1292.               case 139:
  1293.                 (void)fprintf(errfile.fp, "bop occurred before eop!\n"), Putl(errfile, 1);
  1294.                 goto L9998;
  1295.                 break ;
  1296.               case 140:
  1297.                 if (s != 0)
  1298.                     (void)fprintf(errfile.fp, "stack not empty at end of page (level %1d)!\n", s), Putl(errfile, 1);
  1299.                 R101 = true;
  1300.                 flushpage();
  1301.                 goto L9999;
  1302.                 break ;
  1303.               case 141:
  1304.                 if (s == maxssofar) {
  1305.                     maxssofar = s + 1;
  1306.                     if (s == maxs)
  1307.                         (void)fprintf(errfile.fp, "deeper than claimed in postamble!\n"), Putl(errfile, 1);
  1308.                     if (s == stacksize) {
  1309.                         (void)fprintf(errfile.fp, "DVIDOC capacity exceeded (stack size=%1d)\n", stacksize), Putl(errfile, 1);
  1310.                         goto L9998;
  1311.                     }
  1312.                 }
  1313.                 hstack.A[s] = h;
  1314.                 vstack.A[s] = v;
  1315.                 wstack.A[s] = w;
  1316.                 xstack.A[s] = x;
  1317.                 ystack.A[s] = y;
  1318.                 zstack.A[s] = z;
  1319.                 hhstack.A[s] = hh;
  1320.                 vvstack.A[s] = vv;
  1321.                 s = s + 1;
  1322.                 ss = s - 1;
  1323.                 goto L45;
  1324.                 break ;
  1325.               case 142:
  1326.                 if (s == 0)
  1327.                     (void)fprintf(errfile.fp, "Pop illegal at level zero!\n"), Putl(errfile, 1);
  1328.                 else {
  1329.                     s = s - 1;
  1330.                     hh = hhstack.A[s];
  1331.                     vv = vvstack.A[s];
  1332.                     h = hstack.A[s];
  1333.                     v = vstack.A[s];
  1334.                     w = wstack.A[s];
  1335.                     x = xstack.A[s];
  1336.                     y = ystack.A[s];
  1337.                     z = zstack.A[s];
  1338.                 }
  1339.                 ss = s;
  1340.                 goto L45;
  1341.                 break ;
  1342.               case 143:  case 144:  case 145:  case 146:
  1343.                 if (abs(p) >= fontspace.A[curfont]) {
  1344.                     hh = Round(horizconv * (h + p));
  1345.                 } else
  1346.                     hh = hh + Round(horizconv * (p));
  1347.                 q = p;
  1348.                 goto L43;
  1349.                 break ;
  1350.               case 147:  case 148:  case 149:  case 150:
  1351.               case 151:
  1352.                 w = p;
  1353.                 if (abs(p) >= fontspace.A[curfont]) {
  1354.                     hh = Round(horizconv * (h + p));
  1355.                 } else
  1356.                     hh = hh + Round(horizconv * (p));
  1357.                 q = p;
  1358.                 goto L43;
  1359.                 break ;
  1360.               case 152:  case 153:  case 154:  case 155:
  1361.               case 156:
  1362.                 x = p;
  1363.                 if (abs(p) >= fontspace.A[curfont]) {
  1364.                     hh = Round(horizconv * (h + p));
  1365.                 } else
  1366.                     hh = hh + Round(horizconv * (p));
  1367.                 q = p;
  1368.                 goto L43;
  1369.                 break ;
  1370.               default:
  1371.                 if (specialcases(o, p, a))
  1372.                     goto L30;
  1373.                 else
  1374.                     goto L9998;
  1375.             }
  1376.     L41:
  1377.         if (p < 0)
  1378.             p = 255 - ((-1 - p) % 256);
  1379.         else
  1380.             if (p >= 256)
  1381.                 p = p % 256;
  1382.         if ((p < fontbc.A[curfont]) || (p > fontec.A[curfont]))
  1383.             q = 2147483647;
  1384.         else
  1385.             q = width.A[widthbase.A[curfont] + p];
  1386.         if (q == 2147483647) {
  1387.             (void)fprintf(errfile.fp, "character %1d invalid in font \n", p), Putl(errfile, 1);
  1388.             printfont(curfont);
  1389.             if (curfont != nf)
  1390.                 Putchr('!', errfile);
  1391.         }
  1392.         if (o >= 133)
  1393.             goto L30;
  1394.         if (q == 2147483647)
  1395.             q = 0;
  1396.         else
  1397.             hh = hh + pixelwidth.A[widthbase.A[curfont] + p];
  1398.         goto L43;
  1399.     L42:
  1400.         q = signedquad();
  1401.         if ((p > 0) && (q > 0))
  1402.             {
  1403.                 integer    B37 = hh,
  1404.                     B38 = hh + horizrulepixels(q) - 1;
  1405.  
  1406.                 if (B37 <= B38)
  1407.                     for (i = B37; ; i++) {
  1408.                         {
  1409.                             integer    B39 = vv,
  1410.                                 B40 = vv - vertrulepixels(p) + 1;
  1411.  
  1412.                             if (B39 >= B40)
  1413.                                 for (j = B39; ; j--) {
  1414.                                     outchar(xord.A['-'], i, j);
  1415.                                     if (j == B40) break;
  1416.                                 }
  1417.                         }
  1418.                         if (i == B38) break;
  1419.                     }
  1420.             }
  1421.         if (o == 137)
  1422.             goto L30;
  1423.         hh = hh + horizrulepixels(q);
  1424.         goto L43;
  1425.     L43:
  1426.         if ((h > 0) && (q > 0))
  1427.             if (h > 2147483647 - q) {
  1428.                 (void)fprintf(errfile.fp, "arithmetic overflow! parameter changed from %1d to %1d\n", q, 2147483647 - h), Putl(errfile, 1);
  1429.                 q = 2147483647 - h;
  1430.             }
  1431.         if ((h < 0) && (q < 0))
  1432.             if (-h > q + 2147483647) {
  1433.                 (void)fprintf(errfile.fp, "arithmetic overflow! parameter changed from %1d to %1d\n", q, (-h) - 2147483647), Putl(errfile, 1);
  1434.                 q = (-h) - 2147483647;
  1435.             }
  1436.         hhh = Round(horizconv * (h + q));
  1437.         if (abs(hhh - hh) > 2)
  1438.             if (hhh > hh)
  1439.                 hh = hhh - 2;
  1440.             else
  1441.                 hh = hhh + 2;
  1442.         h = h + q;
  1443.         if (abs(h) > maxhsofar) {
  1444.             if (abs(h) > maxh + 99) {
  1445.                 (void)fprintf(errfile.fp, "warning: |h|>%1d!\n", maxh), Putl(errfile, 1);
  1446.                 maxh = abs(h);
  1447.             }
  1448.             maxhsofar = abs(h);
  1449.         }
  1450.         goto L30;
  1451.     L45:
  1452.         ;
  1453.     L30:
  1454.         ;
  1455.     }
  1456. L9998:
  1457.     Putchr('!', errfile),Putchr('\n', errfile);
  1458.     R101 = false;
  1459. L9999:
  1460.     ;
  1461.     return R101;
  1462. }
  1463.  
  1464.  void
  1465. readpostamble()
  1466. {
  1467.     integer    k;
  1468.     integer    p, q, m;
  1469.  
  1470.     postloc = curloc - 5;
  1471.     if (signedquad() != numerator)
  1472.         (void)fprintf(errfile.fp, "numerator doesn't match the preamble!\n"), Putl(errfile, 1);
  1473.     if (signedquad() != denominator)
  1474.         (void)fprintf(errfile.fp, "denominator doesn't match the preamble!\n"), Putl(errfile, 1);
  1475.     if (signedquad() != mag)
  1476.         if (newmag == 0)
  1477.             (void)fprintf(errfile.fp, "magnification doesn't match the preamble!\n"), Putl(errfile, 1);
  1478.     maxv = signedquad();
  1479.     maxh = signedquad();
  1480.     maxs = gettwobytes();
  1481.     totalpages = gettwobytes();
  1482.     do {
  1483.         k = getbyte();
  1484.         if ((k >= 243) && (k < 247)) {
  1485.             p = firstpar(k);
  1486.             definefont(p);
  1487.             Putchr(' ', errfile),Putchr('\n', errfile);
  1488.             k = 138;
  1489.         }
  1490.     } while (!(k != 138));
  1491.     if (k != 249)
  1492.         (void)fprintf(errfile.fp, "byte %1d is not postpost!\n", curloc - 1), Putl(errfile, 1);
  1493.     q = signedquad();
  1494.     if (q != postloc)
  1495.         (void)fprintf(errfile.fp, "bad postamble pointer in byte %1d!\n", curloc - 4), Putl(errfile, 1);
  1496.     m = getbyte();
  1497.     if (m != 2)
  1498.         (void)fprintf(errfile.fp, "identification in byte %1d should be %1d!\n", curloc - 1, 2), Putl(errfile, 1);
  1499.     k = curloc;
  1500.     m = 223;
  1501.     while ((m == 223) && !eofdvifile)
  1502.         m = getbyte();
  1503.     if (!eofdvifile) {
  1504.         (void)fprintf(stderr, " Bad DVI file: signature in byte %1d should be 223!\n", curloc - 1), Putl(output, 1);
  1505.         jumpout();
  1506.     } else
  1507.         if (curloc < k + 4)
  1508.             (void)fprintf(errfile.fp, "not enough signature bytes at end of file (%1d)\n", curloc - k), Putl(errfile, 1);
  1509. }
  1510.  
  1511. /*
  1512. **    Start of program code
  1513. */
  1514. main(_ac, _av)
  1515. int    _ac;
  1516. char    *_av[];
  1517. {
  1518.  
  1519.     argc = _ac;
  1520.     argv = _av;
  1521.     switch (setjmp(J[0].jb)) {
  1522.       case 0:
  1523.         break;
  1524.       case 9999:
  1525.         goto L9999;
  1526.       case 30:
  1527.         goto L30;
  1528.       default:
  1529.         Caseerror(Line);
  1530.     }
  1531.     initialize();
  1532.     options();
  1533.     i = 1;
  1534.     while ((i < namelength) && (basename.A[i - 1] != ' '))
  1535.         i = i + 1;
  1536.     if (i < 5) {
  1537.         basename.A[i - 1] = '.';
  1538.         basename.A[i + 1 - 1] = 'd';
  1539.         basename.A[i + 2 - 1] = 'v';
  1540.         basename.A[i + 3 - 1] = 'i';
  1541.         basename.A[i + 4 - 1] = ' ';
  1542.     }
  1543.     if ((i >= 5) && (i < namelength - 4) && ((basename.A[i - 4 - 1] != '.') || (basename.A[i - 3 - 1] != 'd') || (basename.A[i - 2 - 1] != 'v') || (basename.A[i - 1 - 1] != 'i'))) {
  1544.         basename.A[i - 1] = '.';
  1545.         basename.A[i + 1 - 1] = 'd';
  1546.         basename.A[i + 2 - 1] = 'v';
  1547.         basename.A[i + 3 - 1] = 'i';
  1548.         basename.A[i + 4 - 1] = ' ';
  1549.     }
  1550.     curname = basename;
  1551.     if (testaccess(4, 0)) {
  1552.         opendvifile();
  1553.         if (eofdvifile) {
  1554.             (void)fprintf(stderr, "Input file not found\n"), Putl(output, 1);
  1555.             goto L30;
  1556.         }
  1557.     } else {
  1558.         (void)fprintf(stderr, "Cannot open input file\n"), Putl(output, 1);
  1559.         goto L30;
  1560.     }
  1561.     if (logerrors) {
  1562.         curname = basename;
  1563.         i = 1;
  1564.         while ((i <= namelength) && (curname.A[i - 1] != ' '))
  1565.             i = i + 1;
  1566.         curname.A[i - 3 - 1] = 'e';
  1567.         curname.A[i - 2 - 1] = 'r';
  1568.         curname.A[i - 1 - 1] = 'r';
  1569.         if (testaccess(2, 0))
  1570.             Rewritex(errfile, curname.A);
  1571.         else {
  1572.             (void)fprintf(stderr, "Cannot create error file\n"), Putl(output, 1);
  1573.             goto L30;
  1574.         }
  1575.     } else {
  1576.         (void)strncpy(curname.A, "/dev/null", sizeof(curname.A));
  1577.         if (testaccess(2, 0))
  1578.             Rewritex(errfile, curname.A);
  1579.         else {
  1580.             (void)fprintf(stderr, "Somebody stole /dev/null!\n"), Putl(output, 1);
  1581.             goto L30;
  1582.         }
  1583.     }
  1584.     p = getbyte();
  1585.     if (p != 247) {
  1586.         (void)fprintf(stderr, " Bad DVI file: First byte isn't start of preamble!!\n"), Putl(output, 1);
  1587.         jumpout();
  1588.     }
  1589.     p = getbyte();
  1590.     if (p != 2)
  1591.         (void)fprintf(errfile.fp, "identification in byte 1 should be %1d!\n", 2), Putl(errfile, 1);
  1592.     numerator = signedquad();
  1593.     denominator = signedquad();
  1594.     if (numerator <= 0) {
  1595.         (void)fprintf(stderr, " Bad DVI file: numerator is %1d!\n", numerator), Putl(output, 1);
  1596.         jumpout();
  1597.     }
  1598.     if (denominator <= 0) {
  1599.         (void)fprintf(stderr, " Bad DVI file: denominator is %1d!\n", denominator), Putl(output, 1);
  1600.         jumpout();
  1601.     }
  1602.     horizconv = (numerator / ((real)254000.0)) * (horizresolution / ((real)denominator));
  1603.     vertconv = (numerator / ((real)254000.0)) * (vertresolution / ((real)denominator));
  1604.     mag = signedquad();
  1605.     if (newmag > 0)
  1606.         mag = newmag;
  1607.     else
  1608.         if (mag <= 0) {
  1609.             (void)fprintf(stderr, " Bad DVI file: magnification is %1d!\n", mag), Putl(output, 1);
  1610.             jumpout();
  1611.         }
  1612.     truehorizconv = horizconv;
  1613.     horizconv = truehorizconv * (mag / ((real)1000.0));
  1614.     truevertconv = vertconv;
  1615.     vertconv = truevertconv * (mag / ((real)1000.0));
  1616.     p = getbyte();
  1617.     Putchr('\'', errfile);
  1618.     while (p > 0) {
  1619.         p = p - 1;
  1620.         Putchr(xchr.A[getbyte()], errfile);
  1621.     }
  1622.     Putchr('\'', errfile),Putchr('\n', errfile);
  1623.     n = dvilength();
  1624.     if (n < 53) {
  1625.         (void)fprintf(stderr, " Bad DVI file: only %1d bytes long!\n", n), Putl(output, 1);
  1626.         jumpout();
  1627.     }
  1628.     m = n - 4;
  1629.     do {
  1630.         if (m == 0) {
  1631.             (void)fprintf(stderr, " Bad DVI file: all 223s!\n"), Putl(output, 1);
  1632.             jumpout();
  1633.         }
  1634.         movetobyte(m);
  1635.         k = getbyte();
  1636.         m = m - 1;
  1637.     } while (!(k != 223));
  1638.     if (k != 2) {
  1639.         (void)fprintf(stderr, " Bad DVI file: ID byte is %1d!\n", k), Putl(output, 1);
  1640.         jumpout();
  1641.     }
  1642.     movetobyte(m - 3);
  1643.     q = signedquad();
  1644.     if ((q < 0) || (q > m - 33)) {
  1645.         (void)fprintf(stderr, " Bad DVI file: post pointer %1d at byte %1d!\n", q, m - 3), Putl(output, 1);
  1646.         jumpout();
  1647.     }
  1648.     movetobyte(q);
  1649.     k = getbyte();
  1650.     if (k != 248) {
  1651.         (void)fprintf(stderr, " Bad DVI file: byte %1d is not post!\n", q), Putl(output, 1);
  1652.         jumpout();
  1653.     }
  1654.     postloc = q;
  1655.     firstbackpointer = signedquad();
  1656.     inpostamble = true;
  1657.     readpostamble();
  1658.     inpostamble = false;
  1659.     q = postloc;
  1660.     p = firstbackpointer;
  1661.     startloc = -1;
  1662.     if (p >= 0)
  1663.         do {
  1664.             if (p > q - 46) {
  1665.                 (void)fprintf(stderr, " Bad DVI file: page link %1d after byte %1d!\n", p, q), Putl(output, 1);
  1666.                 jumpout();
  1667.             }
  1668.             q = p;
  1669.             movetobyte(q);
  1670.             k = getbyte();
  1671.             if (k == 139)
  1672.                 pagecount = pagecount + 1;
  1673.             else {
  1674.                 (void)fprintf(stderr, " Bad DVI file: byte %1d is not bop!\n", q), Putl(output, 1);
  1675.                 jumpout();
  1676.             }
  1677.             {
  1678.                 integer    B41 = 0,
  1679.                     B42 = 9;
  1680.  
  1681.                 if (B41 <= B42)
  1682.                     for (k = B41; ; k++) {
  1683.                         count.A[k] = signedquad();
  1684.                         if (k == B42) break;
  1685.                     }
  1686.             }
  1687.             if (startmatch())
  1688.                 startloc = q;
  1689.             p = signedquad();
  1690.         } while (!(p < 0));
  1691.     if (startloc < 0) {
  1692.         (void)fprintf(stderr, " starting page number could not be found!\n"), Putl(output, 1);
  1693.         jumpout();
  1694.     }
  1695.     movetobyte(startloc + 1);
  1696.     oldbackpointer = startloc;
  1697.     {
  1698.         integer    B43 = 0,
  1699.             B44 = 9;
  1700.  
  1701.         if (B43 <= B44)
  1702.             for (k = B43; ; k++) {
  1703.                 count.A[k] = signedquad();
  1704.                 if (k == B44) break;
  1705.             }
  1706.     }
  1707.     p = signedquad();
  1708.     started = true;
  1709.     if (!inpostamble) {
  1710.         while (maxpages > 0) {
  1711.             maxpages = maxpages - 1;
  1712.             (void)fprintf(errfile.fp, "Page "), Putl(errfile, 0);
  1713.             {
  1714.                 integer    B45 = 0,
  1715.                     B46 = startvals;
  1716.  
  1717.                 if (B45 <= B46)
  1718.                     for (k = B45; ; k++) {
  1719.                         (void)fprintf(errfile.fp, "%1d", count.A[k]), Putl(errfile, 0);
  1720.                         if (k < startvals)
  1721.                             Putchr('.', errfile);
  1722.                         else
  1723.                             Putchr(' ', errfile),Putchr('\n', errfile);
  1724.                         if (k == B46) break;
  1725.                     }
  1726.             }
  1727.             if (!dopage()) {
  1728.                 (void)fprintf(stderr, " Bad DVI file: page ended unexpectedly!\n"), Putl(output, 1);
  1729.                 jumpout();
  1730.             }
  1731.             do {
  1732.                 k = getbyte();
  1733.                 if ((k >= 243) && (k < 247)) {
  1734.                     p = firstpar(k);
  1735.                     definefont(p);
  1736.                     k = 138;
  1737.                 }
  1738.             } while (!(k != 138));
  1739.             if (k == 248) {
  1740.                 inpostamble = true;
  1741.                 goto L30;
  1742.             }
  1743.             if (k != 139) {
  1744.                 (void)fprintf(stderr, " Bad DVI file: byte %1d is not bop!\n", curloc - 1), Putl(output, 1);
  1745.                 jumpout();
  1746.             }
  1747.             newbackpointer = curloc - 1;
  1748.             pagecount = pagecount + 1;
  1749.             {
  1750.                 integer    B47 = 0,
  1751.                     B48 = 9;
  1752.  
  1753.                 if (B47 <= B48)
  1754.                     for (k = B47; ; k++) {
  1755.                         count.A[k] = signedquad();
  1756.                         if (k == B48) break;
  1757.                     }
  1758.             }
  1759.             if (signedquad() != oldbackpointer)
  1760.                 (void)fprintf(errfile.fp, "backpointer in byte %1d should be %1d!\n", curloc - 4, oldbackpointer), Putl(errfile, 1);
  1761.             oldbackpointer = newbackpointer;
  1762.         }
  1763.     L30:
  1764.         ;
  1765.     }
  1766. L9999:
  1767.     ;
  1768.     exit(0);
  1769. }
  1770. /*
  1771. **    End of program code
  1772. */
  1773.  
  1774. static FILE *
  1775. Fopen(n, m)
  1776. char    *n, *m;
  1777. {
  1778.     FILE        *f;
  1779.     register char    *s;
  1780.     static char    ch = 'A';
  1781.     static char    tmp[MAXFILENAME];
  1782.     extern int    unlink();
  1783.  
  1784.     if (n == NULL)
  1785.         sprintf(tmp, "/tmp/ptc%d%c", getpid(), ch++);
  1786.     else {
  1787.         strncpy(tmp, n, sizeof(tmp));
  1788.         for (s = &tmp[sizeof(tmp)-1]; *s == ' ' || *s == '\0'; )
  1789.             *s-- = '\0';
  1790.         if (tmp[sizeof(tmp)-1]) {
  1791.             (void)fprintf(stderr, "Too long filename '%s'\n", n);
  1792.             exit(1);
  1793.         }
  1794.     }
  1795.     s = tmp;
  1796.     if ((f = fopen(s, m)) == NULL) {
  1797.         (void)fprintf(stderr, "Cannot open: %s\n", s);
  1798.         exit(1);
  1799.     }
  1800.     if (n == NULL)
  1801.         unlink(tmp);
  1802.     return (f);
  1803. }
  1804. #if !defined(AIX) && !defined(__osf__)
  1805. extern int    rewind();
  1806. #endif
  1807.  
  1808. static void
  1809. Caseerror(n)
  1810.     int    n;
  1811. {
  1812.     (void)fprintf(stderr, "Missing case limb: line %d\n", n);
  1813.     exit(1);
  1814. }
  1815. static int
  1816. Trunc(f)
  1817. real    f;
  1818. {
  1819.     return f;
  1820. }
  1821. static int
  1822. Round(f)
  1823. real    f;
  1824. {
  1825.     extern double floor();
  1826.     return floor((double)(0.5+f));
  1827. }
  1828.